package com.tom.architect02.zframework.sreenAdapter;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import static android.content.Context.WINDOW_SERVICE;

public class LoadViewHelper {
    // 真实参数
    protected float actualDensity;
    protected float actualDensityDpi;
    protected float actualWidth;
    protected float actualHeight;

    // 标准参数
    protected int designWidth;
    protected int designDpi;
    protected float fontSize;
    protected String unit;

    public LoadViewHelper(Context context, int designWidth, int designDpi, float fontSize, String unit) {
        this.designWidth = designWidth;
        this.designDpi = designDpi;
        this.fontSize = fontSize;
        this.unit = unit;
        setActualParams(context);
    }

    public void reset(Context context) {
        setActualParams(context);
    }


    /**
     * 遍历DecorView的每个控件，根据这个控件设置的宽高，再根据设计稿中的宽高，进行等比例缩放。
     * 给每个控件动态设置值。
     * @param view
     */
    public void loadView(View view) {
        if (view instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view;
            if (view.getLayoutParams() != null) {
                // 1.是容器，自身也要适配。
                loadWidthHeightFont(view);
                loadPadding(view);
                loadLayoutMargin(view);
            }
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                if (viewGroup.getChildAt(i) instanceof ViewGroup) { // 子控件还是容器，递归调用
                    loadView(viewGroup.getChildAt(i)); // 继续遍历
                } else { // 2.子控件是View，适配View。
                    View child = viewGroup.getChildAt(i);
                    loadWidthHeightFont(child);
                    loadPadding(child);
                    loadLayoutMargin(child);
                }
            }
        } else {
            // 3.非容器控件，适配。
            loadWidthHeightFont(view);
            loadPadding(view);
            loadLayoutMargin(view);
        }
    }

    public void loadPadding(View view) {
        view.setPadding(setValue(view.getPaddingLeft()), setValue(view.getPaddingTop()), setValue(view.getPaddingRight()), setValue(view.getPaddingBottom()));
    }
    public void loadLayoutMargin(View view) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        ViewGroup.MarginLayoutParams marginLayoutParams;
        if (params instanceof ViewGroup.MarginLayoutParams) {
            marginLayoutParams = (ViewGroup.MarginLayoutParams) params;
            marginLayoutParams.leftMargin = setValue(marginLayoutParams.leftMargin);
            marginLayoutParams.topMargin = setValue(marginLayoutParams.topMargin);
            marginLayoutParams.rightMargin = setValue(marginLayoutParams.rightMargin);
            marginLayoutParams.bottomMargin = setValue(marginLayoutParams.bottomMargin);
            view.setLayoutParams(marginLayoutParams);
        }
    }

    public void loadView1(View view) {
        loadView1(view, new SimpleConversion());
    }

    public final void loadView1(View view, IConversion conversion) {
        if (view instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view;
            conversion.transform(viewGroup, this);
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                if (viewGroup.getChildAt(i) instanceof ViewGroup) {
                    loadView1(viewGroup.getChildAt(i), conversion);
                } else {
                    conversion.transform(viewGroup.getChildAt(i), this);
                }
            }
        } else {
            conversion.transform(view, this);
        }

    }

    public void loadWidthHeightFont(View view) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (layoutParams.width > 0) {
            layoutParams.width = setValue(layoutParams.width);
        }
        if (layoutParams.height > 0) {
            layoutParams.height = setValue(layoutParams.height);
        }
        loadViewFont(view);
    }

    private int setValue(int value) {
        if (value == 0) { // warp_content
            return 0;
        } else if (value == 1) { // match_parent
            return 1;
        }
        return (int) calculateValue(value);
    }

    // 计算具体的值，做适配。
    private float calculateValue(float value) {
        if ("px".equals(unit)) {
            return value * ((float) actualWidth / (float) designWidth);
        } else if ("dp".equals(unit)) { // 得到已px为单位的真实宽高值。
            int dip = Dp2pxUtils.px2dip(actualDensity, value);
            value = ((float) designDpi / 160) * dip;
            return value * ((float) actualWidth / (float) designWidth);
        }
        return 0;
    }
    // 针对TextView。设置字体大小适配。
    private void loadViewFont(View view) {
        if ((view instanceof TextView)) {
            TextView textView = (TextView) view;
            ((TextView) view).setTextSize(0, setFontSize(textView));
        }
    }

    private float setFontSize(TextView textView) {
        // 字体大小适配
        return calculateValue(textView.getTextSize() * fontSize);
    }

    private void setActualParams(Context context) {
        float[] actualScreenInfo = getScreenInfo(context);
        if (actualScreenInfo.length == 4) {
            actualWidth = actualScreenInfo[0];
            actualHeight = actualScreenInfo[1];
            actualDensity = actualScreenInfo[2];
            actualDensityDpi = actualScreenInfo[3];
        }
    }

    public static float[] getScreenInfo(Context context) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) context.getSystemService(WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        return new float[]{displayMetrics.widthPixels, displayMetrics.heightPixels, displayMetrics.density, displayMetrics.densityDpi};
    }
}
